package com.yl.nettylearning.nio;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author yangl
 */
public class FileChannelDemo2{
    /**
     * fileChannel读取数据
     */

    public static void main(String[] args) throws Exception {
        //创建文件输入流
        File file = new File("D:\\test01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        //通过fileInputStream获取对应的fileChannel--实际类型fileChannelImpl
        FileChannel fileChannel = fileInputStream.getChannel();
        //创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
        //将通道的数据读入到buffer中
        int read = fileChannel.read(byteBuffer);
        //将缓冲区的字节转成字符串
        System.out.println(new String(byteBuffer.array()));
        fileInputStream.close();
    }

}
